package com.markerhub.modules.admin;

import cn.hutool.core.map.MapUtil;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.AppUserSetting;
import com.markerhub.modules.common.BaseController;
import com.markerhub.service.AppBlueLabelService;
import com.markerhub.service.AppUserService;
import com.markerhub.service.AppUserSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminUserController extends BaseController {

    @Resource
    AppUserService appUserService;


    @Resource
    AppUserSettingService mAppUserSettingService;

    @GetMapping("/getUserList")
    public Result list(AppUserSetting appOrder) {

        Object pageAdmin = mAppUserSettingService.pageAdmin(getPage(), appOrder);
        return Result.success(MapUtil.builder()
                .put("pageData", pageAdmin)
                .build()
        );
    }


    @GetMapping("/user/info/{userId}")
    public Result getCourse(@PathVariable String userId) {
        AppUserSetting user = appUserService.getUser(userId);
        return Result.success(user);

    }


    @PostMapping("/user/delete")
    public Result getUserSave(@RequestBody String[] ids) {
        List<String> userIds=new ArrayList<>();
        for (String string:ids){
            userIds.add(string);
        }
        int code = appUserService.delUserId(userIds);
        if (code > 0) {
            return Result.success("成功"
            );
        } else {
            return Result.fail("失败"
            );
        }
    }

    @Resource
    AppBlueLabelService appBlueLabelService;

    @PostMapping("/user/save")
    public Result getUserSave(@Validated @RequestBody AppUserSetting userSetting) {
        log.info("/user/save 22222222222222 "+userSetting.toString(),"");
        if (userSetting.getId()==null){
            // 进行注册
            AppUserSetting user = new AppUserSetting();
            user.setUsername(userSetting.getUsername());
            user.setTel(userSetting.getTel());
            user.setIdcard(userSetting.getIdcard());
            user.setLocations(userSetting.getLocations());
            user.setArea(userSetting.getArea());
            user.setLeader(userSetting.getLeader());
            appUserService.save(user);

            return Result.success("ok"
            );
        }else{
            appUserService.updateUser(userSetting);
            return Result.success("ok"
            );
        }

    }

}
